#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import datetime
import wsgiref.handlers

from google.appengine.ext import webapp
from google.appengine.api import users

# IT
# import models.trackingInfo as TIM
import controllers.trackingInfo as TIC

# SO
import controllers.shippingOrder as SOC

# Manifest
import controllers.manifest as MC

# MP
import controllers.milestonePlan as MPC
import controllers.MPTemplates as MPTC


# Role
import controllers.role as RC

# User
import models.user as UM
import controllers.decorator as dc
import controllers.user as UC

# Stats
import controllers.stats as SC

# Cities
import controllers.city as CC


# Cron Jobs
import controllers.versionLeech as VLC

# VersionLeech
import controllers.mpChecker as MPCC

# sys
import sys

#Opions
import controllers.option as OptC

class MainHandler(webapp.RequestHandler):

  @dc.decorate
  def get(self):
    now = datetime.datetime.now()+datetime.timedelta(hours = 2)
    response='''<h2>Welcome <a href="%s">%s</a></h2> \n
	Today is <i>%s</i>. We have <i>%s</i> of server time.\n
	<br/><br/>\n
	<b>We wish you a good day.</b>
	\n\n
	''' % (
	UM.UserModel.userAcc(), UM.UserModel.userAcc(), now.strftime("%A, %d %B %Y"), now.strftime("%H:%M"))
    self.response.out.write(response)

def main():
  sys.path.append('.')
  application = webapp.WSGIApplication([('/', MainHandler),
                  (r'/stats/(succSO|delayDist|statDist|averMPDelay)/(all|lastyear|lastmonth|lastweek|lastday)', SC.StatsContr),

                  (r'/so/(show1)/id/([1-9][0-9]*)',       SOC.SOContr),
                  (r'/so/(showAll)',                      SOC.SOContr),
                  (r'/so/(edit)/id/([1-9][0-9]*)',        SOC.SOContr),
                  (r'/so/(new)',                          SOC.SOContr),
                  (r'/so/(populate)',                     SOC.SOContr),
                  (r'/so/(delete)/id/([1-9][0-9]*)',      SOC.SOContr),
                                        
                  (r'/manifest/(show1)/id/([1-9][0-9]*)',   MC.ManifestContr),
                  (r'/manifest/(showAll)',                  MC.ManifestContr),
                  (r'/manifest/(edit)/id/([1-9][0-9]*)',    MC.ManifestContr),
                  (r'/manifest/(new)',                      MC.ManifestContr),
                  (r'/manifest/(delete)/id/([1-9][0-9]*)',  MC.ManifestContr),
                                        
                  (r'/role/(showAll)',      RC.RoleContr),
                                        
                  (r'/mp/(show1)/id/([1-9][0-9]*)',              MPC.MPContr),
                  (r'/mp/(showTemplate)',       MPC.MPContr),
                  (r'/mp/(upload)',       MPTC.MPTemplateContr),
                                        
                  (r'/ti/(new)',                        TIC.TIContr),
                  (r'/ti/(show1)/id/([1-9][0-9]*)',     TIC.TIContr),
                  (r'/ti/(showAll)/SOid/([1-9][0-9]*)', TIC.TIContr),
                  (r'/ti/(showAllMine)',                TIC.TIContr),
                  (r'/ti/(edit)/id/([1-9][0-9]*)',      TIC.TIContr),
                  (r'/ti/(delete)/id/([1-9][0-9]*)',    TIC.TIContr),
                  (r'/ti/(populate)',                   TIC.TIContr),
                                        
                  (r'/user/(show1)/id/([1-9][0-9]*)',   UC.UserContr),
                  (r'/user/(showAll)',                  UC.UserContr),
                  (r'/user/(edit)/id/([1-9][0-9]*)',    UC.UserContr),
                  (r'/user/(delete)/id/([1-9][0-9]*)',  UC.UserContr),
                  (r'/user/(new)',                      UC.UserContr),
                  (r'/user/(populate)',                 UC.UserContr),
              
                  (r'/options/(showAll)',                 OptC.OptionContr),
                  (r'/options/(new)',                     OptC.OptionContr),
                  (r'/options/(delete)/id/([1-9][0-9]*)',   OptC.OptionContr),
                  (r'/options/(populate)',                     OptC.OptionContr),
                  
                  (r'/cities/(show1)/id/([1-9][0-9]*)',   CC.CityContr),
                  (r'/cities/(showAll)',                  CC.CityContr),
                  (r'/cities/(edit)/id/([1-9][0-9]*)',    CC.CityContr),
                  (r'/cities/(delete)/id/([1-9][0-9]*)',  CC.CityContr),
                  (r'/cities/(new)',                      CC.CityContr),
                  
                  
                  (r'/cron/(purgeVersionLeech)',        VLC.VersionLeechContr),
                  (r'/cron/(checkDelays)',              MPCC.MPCheckerContr)
                                          ],
                                       debug=True)
  wsgiref.handlers.CGIHandler().run(application)


if __name__ == '__main__':
  main()
